package com.nolon.flange.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView
import com.nolon.flange.R
import com.nolon.flange.databinding.ItemRawSpecBinding
import com.nolon.flange.databinding.ItemRawSpecHeaderBinding
import com.nolon.flange.extensions.numberF
import com.nolon.flange.extensions.numberI
import com.nolon.flange.model.RawInfo
import com.nolon.flange.tools.F
import com.nolon.flange.tools.I

class RawAdapter(val acc: AppCompatActivity) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var tableHeaderColor: Int = ContextCompat.getColor(acc, R.color.table_1_header)
    var tableRowOddColor: Int = ContextCompat.getColor(acc, R.color.table_1_row_1)
    var tableRowEvenColor: Int = ContextCompat.getColor(acc, R.color.table_1_row_2)

    private val tableRows: ArrayList<RawSpecViewHolder> = arrayListOf()

    private val prefMaxComposition = PreferenceManager.getDefaultSharedPreferences(acc)
        .getString(acc.getString(R.string.pref_max_composition), "5")

    private val prefRawLength = PreferenceManager.getDefaultSharedPreferences(acc)
        .getString(acc.getString(R.string.pref_raw_length), "5980")

    private val prefUnitPrice = PreferenceManager.getDefaultSharedPreferences(acc)
        .getString(acc.getString(R.string.pref_unit_price), "50")

    var rawInfoList: List<RawInfo> = listOf()
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    fun getRawInfo(): List<RawInfo> {
        return arrayListOf<RawInfo>().apply {
            tableRows.forEach {
                add(it.getRawInfoFromView())
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == HEADER_TYPE) {
            val binding =
                ItemRawSpecHeaderBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            RawSpecHeaderViewHolder(binding)
        } else {
            val binding =
                ItemRawSpecBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            val row = RawSpecViewHolder(binding)
            row.also { tableRows.add(row) }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (position == 0 && holder is RawSpecHeaderViewHolder) {
            holder.binding.layout.setBackgroundColor(tableHeaderColor)
        }
        if (position > 0 && holder is RawSpecViewHolder) {
            holder.rawInfo = rawInfoList[position - 1]
            with(holder.binding) {
                textNumber.text = position.toString()
                textType.text = holder.rawInfo.materialSpec
                textActualSpec.text = holder.rawInfo.actualSpec.toString()
                editMaxComb.hint = prefMaxComposition ?: 5.toString()
                editLength.hint = prefRawLength ?: 5980.toString()
                editUnitPrice.hint = prefUnitPrice ?: 50.toString()
                if (position.rem(2) == 0) {
                    layout.setBackgroundColor(tableRowEvenColor)
                } else {
                    layout.setBackgroundColor(tableRowOddColor)
                }
            }
        }
    }

    override fun getItemViewType(position: Int) = if (position == 0) HEADER_TYPE else CONTENT_TYPE

    override fun getItemCount() = rawInfoList.size + 1

    class RawSpecHeaderViewHolder(val binding: ItemRawSpecHeaderBinding) :
        RecyclerView.ViewHolder(binding.root)

    class RawSpecViewHolder(val binding: ItemRawSpecBinding) :
        RecyclerView.ViewHolder(binding.root) {
        lateinit var rawInfo: RawInfo
        fun getRawInfoFromView(): RawInfo {
            val context = binding.root.context
            val prefMaxComposition = PreferenceManager.getDefaultSharedPreferences(context)
                .getString(context.getString(R.string.pref_max_composition), "5").I(5)

            val prefRawLength = PreferenceManager.getDefaultSharedPreferences(context)
                .getString(context.getString(R.string.pref_raw_length), "5980").I(5980)

            val prefUnitPrice = PreferenceManager.getDefaultSharedPreferences(context)
                .getString(context.getString(R.string.pref_unit_price), "50").F(50f)

            val max = binding.editMaxComb.numberI(prefMaxComposition)
            val len = binding.editLength.numberI(prefRawLength)
            val unit = binding.editUnitPrice.numberF(prefUnitPrice)
            return rawInfo.copy(length = len, unitPrice = unit, maxCombination = max)
        }
    }

    companion object {
        const val HEADER_TYPE = 0
        const val CONTENT_TYPE = 1
    }
}